package com.lovecar.view;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.lovecar.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class WeatherAcitvity extends Activity {
	private EditText cityId;
	private Button serachButton;
	private TextView weaherResult;
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.weather);	
		serachButton=(Button) findViewById(R.id.button1);
		cityId=(EditText) findViewById(R.id.cityId);
		weaherResult=(TextView) findViewById(R.id.weatherResult);
		
		serachButton.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					String s=test(cityId.getText().toString());
					weaherResult.setText(s);
				}
			});
		
		// TODO Put your code here
	}
	/***
	 * {"weatherinfo":{"weather6":"阵雨转小雨","weather5":"多云","weather4":"多云",
	 * "index_d":"较凉爽，建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"
	 * ,"city":"广州",
	 * "img_single":"2","index_cl":"较适宜","img2":"99","img1":"2","index":"暖","tempF1":"73.4℉~60.8℉",
	 * "img_title10":"多云","img_title11":"阵雨","img_title12":"小雨","index_xc":"适宜",
	 * "index_co":"舒适","img_title_single":"阴","city_en":"guangzhou","date_y":"2011年11月25日",
	 * "index48_d":"较凉爽，建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。",
	 * "img9":"1","img7":"1","img8":"99","img5":"1","img6":"0","img3":"1","img4":"99",
	 * "fx1":"微风",
	 * "st5":"25","st6":"18","st3":"24","date":"辛卯年","st4":"16","st1":"24","st2":"15",
	 * "temp1":"23℃~16℃",
	 * "tempF6":"78.8℉~60.8℉","temp2":"24℃~16℃","temp3":"25℃~17℃",
	 * "index48":"暖","tempF4":"77℉~66.2℉","tempF5":"78.8℉~66.2℉","index_ls":"不太适宜",
	 * "tempF2":"75.2℉~60.8℉","tempF3":"77℉~62.6℉","index_tr":"很适宜","index_ag":"易发","index48_uv":"弱",
	 * "fl1":"小于3级","fl5":"小于3级","fl4":"小于3级","temp6":"26℃~16℃","fl3":"小于3级","temp5":"26℃~19℃","fl2":"小于3级",
	 * "cityid":"101280101","temp4":"25℃~19℃","img12":"7","img_title7":"多云","img_title6":"晴",
	 * "fl6":"小于3级","img_title5":"多云","fchh":"08",
	 * "img_title4":"多云","img10":"99","img_title9":"多云","img11":"3",
	 * "img_title8":"多云","fx2":"微风","wind1":"微风",
	 * "weather1":"阴",
	 * "wind3":"微风","weather2":"多云",
	 * "wind2":"微风","weather3":"多云转晴",
	 * "wind5":"微风","img_title3":"多云",
	 * "index_uv":"最弱",
	 * "wind4":"微风",
	 * "img_title2":"阴",
	 * "img_title1":"阴","wind6":"微风","week":"星期五"}}

	 * **/
	public static String test(String cityId)
	{
		StringBuffer result=new StringBuffer("");
		try {
			Document doc = Jsoup.connect("http://m.weather.com.cn/data/"+cityId+".html").get();
			String weather=doc.getElementsByTag("body").text();			
			org.json.JSONObject jo=new JSONObject(weather);
			
			org.json.JSONObject weatherinfo=new JSONObject(jo.get("weatherinfo").toString());
			
			result.append("温度："+weatherinfo.get("temp1")).append("\n");
			result.append("时间："+weatherinfo.get("date_y")).append("\n");
			result.append("穿衣指数："+weatherinfo.get("index_d")).append("\n");
			result.append("紫外线："+weatherinfo.get("index_uv")).append("\n");
			result.append("风力："+weatherinfo.get("fx1")).append("\n");
			result.append("天气："+weatherinfo.get("weather1")).append("\n");
			result.append("city："+weatherinfo.get("city")).append("\n");
			System.out.println(result.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			result.append("程序异常：");
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			result.append("程序异常：");
			e.printStackTrace();
		}
		return result.toString();
	}
}
